package com.example.lighttest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HomeScreen extends Activity {
	
	URLAccessor accessor = new URLAccessor();
	String arduinoURL = "http://134.82.134.69/arduino";
	String read_info = null;
	int DARK_CUTOFF = 400;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.ThreadPolicy policy = new StrictMode.
        		ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 
        try {
        	URL url = new URL(arduinoURL + "/mode/7/output");
        	HttpURLConnection con = (HttpURLConnection) url
        			.openConnection();
        	readStream(con.getInputStream());
        } catch (Exception e) {
        	e.printStackTrace();
        }
        try {
        	URL url = new URL(arduinoURL + "/mode/5/output");
        	HttpURLConnection con = (HttpURLConnection) url
        			.openConnection();
        	readStream(con.getInputStream());
        } catch (Exception e) {
        	e.printStackTrace();
        }
        setContentView(R.layout.activity_home_screen);
        updateSensor();
        Button onButton = (Button) findViewById(R.id.on_button);
        Button offButton = (Button) findViewById(R.id.off_button);
        
        onButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	try {
            		  URL url = new URL(arduinoURL + "/digital/7/1");
            		  HttpURLConnection con = (HttpURLConnection) url
            		    .openConnection();
            		  read_info = readStream(con.getInputStream());
            		  System.out.println(read_info);
            		  } catch (Exception e) {
            		  e.printStackTrace();
            		}
            	/*try {
            		wait(500);
            	} catch (Exception e) {
            		e.printStackTrace();
            	}*/
            	try {
          		  URL url = new URL(arduinoURL + "/digital/7/0");
          		  HttpURLConnection con = (HttpURLConnection) url
          		    .openConnection();
          		  readStream(con.getInputStream());
          		  } catch (Exception e) {
          		  e.printStackTrace();
          		} updateSensor();
        }});
        
        offButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	try {
          		  URL url = new URL(arduinoURL + "/digital/5/1");
          		  HttpURLConnection con = (HttpURLConnection) url
          		    .openConnection();
          		  readStream(con.getInputStream());
          		  } catch (Exception e) {
          		  e.printStackTrace();
          		}
            	/*try {
            		wait(500);
            	} catch (Exception e) {
            		e.printStackTrace();
            	}*/
            	try {
          		  URL url = new URL(arduinoURL + "/digital/5/0");
          		  HttpURLConnection con = (HttpURLConnection) url
          		    .openConnection();
          		  readStream(con.getInputStream());
          		  } catch (Exception e) {
          		  e.printStackTrace();
          		} updateSensor();
        }});
    } 
       
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_screen, menu);
        return true;
    }
    
    private String readStream(InputStream in) {
    	  BufferedReader reader = null;
    	  String ret_string = "";
    	  try {
    	    reader = new BufferedReader(new InputStreamReader(in));
    	    String line = "";
    	    while ((line = reader.readLine()) != null) {
    	      //System.out.println(line);
    	      ret_string += line;
    	    }
    	  } catch (IOException e) {
    	    e.printStackTrace();
    	  } finally {
    	    if (reader != null) {
    	      try {
    	    	reader.close();
    	      } catch (IOException e) {
    	        e.printStackTrace();
    	        }
    	    }
    	  }  
    	  return ret_string;
    	} 
    private void updateSensor() {
    	String read_string = null;
    	String number_string = null;
    	String update_text = null;
    	int sensor_val;
    	try {
    		URL url = new URL(arduinoURL + "/analog/0");
    		HttpURLConnection con = (HttpURLConnection) url.openConnection();
    		read_string = readStream(con.getInputStream());
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	System.out.println("UpdateSensor method...");
    	System.out.println(read_string);
    	number_string = read_string.substring(20);
    	sensor_val = Integer.parseInt(number_string);
    	if (sensor_val <= DARK_CUTOFF) {
    		update_text = "My sensor indicates it's bright.";
    	} else {
    		update_text = "My sensor indicates it's dark.";
    	}
    	TextView t = (TextView)findViewById(R.id.light_indicator);
    	t.setText(update_text);
    }
}
